<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>小类详情</title>
    <!-- 引入 Element UI 的样式 -->
    <link rel="stylesheet" href="../../assets/css/element-ui.css" />
    <link rel="stylesheet" href="../../assets/css/styles.css" />
    <script src="../../assets/js/cdn.js"></script>
    <link rel="stylesheet" href="../../assets/fontawesome-free-6.7.2-web/css/all.min.css" />
  </head>
  <body>
    <div id="app">
      <!-- <div class="header">
        <div class="container header-content">
          <div class="logo">订单管理系统</div>
          <div style="position: absolute; left: 50%;">
            <el-button type="primary" size="large" @click="openMenu('../../index.html')">
              首页
            </el-button>
            <el-dropdown size="large" @command="openMenu">
              <el-button type="primary" size="large">
                业务中心<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-for="item in businessCenters" :command="item.path">{{item.name}}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
          <div class="user-info" v-if="currentUser.name">
            欢迎，{{ currentUser.name }}
            <el-button type="primary" size="mini" @click="exit(2)">登出</el-button>
          </div>
        </div>
      </div> -->
      <lx-header slot="header" v-on:toggle="isToggle = !isToggle"></lx-header>
      <div class=" main-content">
        <div class="content" style="width: 100%">

          <h1 class="module-title">基本信息</h1>
          <div class="center-grid mb-20">
            <el-descriptions :column="1">
              <el-descriptions-item v-for="item in baseInfoHeader" :key="item.fieldApi" :label="item.label">
                <template v-if="item.fieldApi === 'bigConfigCode'">
                    <div>{{ formInfo.bigConfigCode}}-{{getOptionsStr(formInfo.bigConfigCode)}}</div>
                </template>
                <template v-else-if="item.fieldApi === 'smallConfigName'">
                  <div>{{ formInfo.smallConfigCode}}-{{formInfo.smallConfigName}}</div>
                </template>
                <template v-else-if="item.fieldApi === 'configType'">
                  <div>{{ getConfigTypeOptionsStr(formInfo.configType) }}</div>
                </template>
                <template v-else>
                    <div>{{ formInfo[item.fieldApi] ? formInfo[item.fieldApi]:'-' }}</div>
                </template>
              </el-descriptions-item>
            </el-descriptions>
          </div>

          <h1 class="module-title">选项配置信息</h1>
          <div class="center-grid mb-20">
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button type="primary" plain size="mini" @click="handleAdd">新增</el-button>
              </el-col>
            </el-row>
            <el-table v-loading="loading" :data="tableData">
              <el-table-column v-for="(item, index) in tableColumnData" :key="index" :label="item.fieldName" min-width="150" :width="item.width">
                <template slot-scope="scope">
                  <template v-if="item.fieldApi === 'configType'"> {{ getConfigTypeOptionsStr(scope.row[item.fieldApi]) }} </template>
                  <template v-else> {{ scope.row[item.fieldApi] }} </template>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" :resizable="false" :show-overflow-tooltip="false" width="180" fixed="right">
                <template slot-scope="scope">
                  <el-button size="mini" type="text"  @click="smallConfigOptionDetailView(scope.row)">详情</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
      <el-dialog title="添加选项配置" :visible.sync="open" :fullscreen="true" width="800px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="小类名称" prop="smallConfigName">
            <el-input v-model="form.smallConfigName" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="选项配置编码" prop="optionCode">
            <el-input v-model="form.optionCode" placeholder="请输入选项配置编码"></el-input>
          </el-form-item>
          <el-form-item label="选项配置名称" prop="optionName">
            <el-input v-model="form.optionName" placeholder="请输入选项配置名称"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
    <script src="../../assets/js/constant.js"></script>
    <script src="../../assets/js/utils.js"></script>
    <script src="../../assets/js/auth.js"></script>
    <script src="../../assets/js/axios.min.js"></script>
    <!-- 引入 Vue.js -->
    <script src="../../assets/js/vue.js"></script>
    <!-- 引入 Element UI 的 JS 文件 -->
    <script src="../../assets/js/element-ui.js"></script>

    <script type="module">
      import LxHeader from '../../pages/components/LxHeader/index.js'
      Vue.component('lx-header', LxHeader)
      checkAuthentication(2)
      new Vue({
        el: '#app',
        components: {},
        data() {
         return {
            currentUser: JSON.parse(localStorage.getItem('currentUser')) || {},
            userMenuList: JSON.parse(localStorage.getItem('userMenuList')) || [],
            businessCenters: [
                { name: '订单中心', path: '../index.html' },
                { name: '用户中心', path: '#' },
                { name: '产品中心', path: '#' },
                { name: '库存中心', path: '#' },
                { name: '财务中心', path: '#' },
                { name: '数据中心', path: '#' },
                { name: '系统中心', path: '#' },
                { name: '消息中心', path: '#' }
            ],
            baseInfoHeader: [
              { label: '大类名称', fieldApi: 'bigConfigCode' },
              { label: '小类名称', fieldApi: 'smallConfigName' },
              { label: '选配类型', fieldApi: 'configType' },
            ],
            optionsObj: [],
            formInfo: {},
            detailDialog: false, // 详情
            smallConfigCode: '',
            root_host: root_host,
            request_host: request_host,
            configTypeOptions: [
                 { configTypeName: '固定', configTypeCode: 0 },
                 { configTypeName: '单选', configTypeCode: 1 },
                 { configTypeName: '多选', configTypeCode: 2 },
            ],
             form: {},
             // 表单校验
             rules: {},
             // 是否显示弹出层
             open: false,
             // 遮罩层
             loading: true,
             queryParams: {
                 pageNo: 1,
                 pageSize: 10,
             },
             tableData: [],
             tableColumnData: [
                 {
                     fieldName: '选项配置编号',
                     fieldApi: 'optionCode'
                 },
                 {
                     fieldName: '选项配置名称',
                     fieldApi: 'optionName'
                 },
             ],
          }
        },
        created() {
            this.init()
        },
        methods: {
          exit,
          init() {
            this.smallConfigCode = getUrlParameter('smallConfigCode')
            this.form.smallConfigCode = this.smallConfigCode
            this.getDetails()
            this.getBigConfigList()
            this.getSmallConfigOptionList()
          },
          getBigConfigList() {
              axios.get(`${this.request_host}/system/bigConfig/allList`).then((response) => {
                  if (response.status == 200) {
                      this.optionsObj = response.data.data
                  }
              })
          },
          getSmallConfigOptionList() {
              this.loading = true
              this.queryParams.smallConfigCode = getUrlParameter('smallConfigCode')
              try {
                  axios.get(`${this.request_host}/system/smallConfigOptions/allList`,{ params: this.queryParams }).then((response) => {
                      if (response.status == 200) {
                          this.tableData = response.data.data
                          this.total = response.data.total
                          this.loading = false
                      }
                  })
              } catch (error) {
                  console.error('请求失败:', error)
              }
          },
          getDetails() {
            // 这里替换为实际的API调用
            axios.get(`${this.request_host}/system/smallConfig/` + this.smallConfigCode)
                .then(response => {
                    this.formInfo = response.data.data
                    this.form.smallConfigName = this.formInfo.smallConfigName
                });
          },
          getOptionsStr(value) {
            for (const element of this.optionsObj) {
                if(element.configCode == value){
                    return element.configName
                }
            }
            return ''
          },
          // 获取选配类型描述
          getConfigTypeOptionsStr(value) {
              for (const element of this.configTypeOptions) {
                  if(element.configTypeCode == value){
                      return element.configTypeName
                  }
              }
              return ''
          },
          /** 新增按钮操作 */
          handleAdd() {
              this.resetQuery()
              this.open = true
          },
          resetQuery() {
              this.queryParams = {
                  pageNo: 1,
                  pageSize: 10,
              }
          },
          /** 提交按钮 */
          submitForm() {
              this.$refs["form"].validate(valid => {
                  if (valid) {
                      // 新增
                      axios.post(`${this.request_host}/system/smallConfigOptions`, this.form)
                          .then(response => {
                              if (response.data.code == 200){
                                  this.$message.success("提交成功")
                                  this.cancel();
                                  this.getSmallConfigOptionList()
                                  const params = `optionCode=`+this.form.optionCode;
                                  const url = `small_config_option_item.html?${params}`;
                                  window.open(url, '_blank');
                              }else {
                                  this.$message.error(response.data.msg)
                              }

                          })
                  }
              })
          },
          // 取消按钮
          cancel() {
              this.open = false
          },
          smallConfigOptionDetailView(row) {
              const optionCode = row.optionCode;
              const params = `optionCode=`+optionCode;
              const url = `small_config_option_item.html?${params}`;
              window.open(url, '_blank');
          },
          openMenu(url) {
            window.open(url)
          },
        }
      })
    </script>
    <style>
      .el-table th.el-table__cell {
        background-color: #f2f2f2; /* 例如，设置为浅灰色 */
      }
    </style>
  </body>
</html>
